package com.pgytesting.secondhandcar.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.pgytesting.secondhandcar.model.Power;
import com.pgytesting.secondhandcar.service.PowerService;
import com.pgytesting.secondhandcar.vo.ResponseMessage;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/admin/power")
public class PowerController {

	@Autowired
	private PowerService powerService;
	
	/**
	 * 获取权限列表
	 * @param power
	 * @return
	 */
	@RequestMapping(value = "/getListObj",method = RequestMethod.POST)
	public ResponseMessage getListObj() {
		try {
			Power power = new Power();
	    	power.setStatus(0);
			return ResponseMessage.successResponse(powerService.queryAll(power));			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("获取权限列表出现异常");
		}
	}
	
	/**
	 *获取权限信息
	 * @param power
	 * @return
	 */
	@RequestMapping(value = "/getObj",method = RequestMethod.POST)
	public ResponseMessage getObj(@RequestBody Power power) {
		try {
			return ResponseMessage.successResponse(powerService.queryObj(power));			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("获取权限信息出现异常");
		}
	}
	
	/**
	 * 添加权限
	 * @param power
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value = "/insertObj",method = RequestMethod.POST)
	public ResponseMessage insertObj(@RequestBody @Valid Power power, BindingResult bindingResult) {
		try {
			if (bindingResult.hasErrors()) {
				List<FieldError> ls = bindingResult.getFieldErrors();
				Map<String,String> map = new HashMap<String,String>();
	            for (int i = 0; i < ls.size(); i++) {
	            	map.put(ls.get(i).getField(), ls.get(i).getDefaultMessage());
	            }
	            return ResponseMessage.errorResponse(map);
	        }
			powerService.insert(power);
			return ResponseMessage.successResponse("添加权限数据成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("添加权限信息出现异常");
		}
	}
	
	/**
	 * 更新权限
	 * @param power
	 * @return
	 */
	@RequestMapping(value = "/updateObj",method = RequestMethod.POST)
	public ResponseMessage updateObj(@RequestBody Power power) {
		try {
			if(power.getId() == null) {
				return ResponseMessage.errorResponse("权限id不能为空");
			}
			powerService.update(power);
			return ResponseMessage.successResponse("更新权限数据成功");			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("更新权限信息出现异常");
		}
	}
	
	/**
	 * 删除权限
	 * @param power
	 * @return
	 */
	@RequestMapping(value = "/deleteObj",method = RequestMethod.POST)
	public ResponseMessage deleteObj(@RequestBody Power power) {
		try {
			if(power.getId() == null) {
				return ResponseMessage.errorResponse("权限id不能为空");
			}
			powerService.deleteObj(power.getId());
			return ResponseMessage.successResponse("删除权限数据成功");			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("删除权限信息出现异常");
		}
	}
}
